const Draw=function(card) {
    this.lang="cn";
    this.card=card;
}
Draw.prototype={
    draw() {
        if (this.lang == "cn") {
            var lang = "txt";
            this.card.ifm.name = this.card.ifm_cn.name;
            this.card.ifm.value = this.card.ifm_cn.value;
            this.card.ifm.lb_value = this.card.ifm_cn.lb_value;
        }
        else if (this.lang == "jp") {
            var lang = "jp";
            this.card.ifm.name = this.card.ifm_jp.name;
            this.card.ifm.value = this.card.ifm_jp.value;
            this.card.ifm.lb_value = this.card.ifm_jp.lb_value;
        }
        else {
            var lang = "en";
            this.card.ifm.name = this.card.ifm_en.name;
            this.card.ifm.value = this.card.ifm_en.value;
            this.card.ifm.lb_value = this.card.ifm_en.lb_value;
        }
        var c = wx.createCanvasContext("card");
        var bg = document.getElementById("bg");
        var pic = document.getElementById("pic");
        var flash = document.getElementById("flash");
        c.drawImage(bg, 0, 0, 813, 1185);

        var pics1 = [101, 220, 614, 616];
        var pics2 = [56, 213, 702, 528];

        if (this.card.ifm.type3 == "lb") {
            var pics = pics2;
        }
        else {
            var pics = pics1;
        }

        if (pic.src) {
            if (this.card.ls) {
                c.drawImage(pic, pics[0], pics[1], pics[2], pics[3]);
                c.globalAlpha = this.card.alpha;
                if (this.card.flash && this.card.rarity != "N") {
                    c.drawImage(flash, pics[0], pics[1], pics[2], pics[3]);
                }
                c.globalAlpha = 1;
            }
            else {
                var w = Math.max(1, Math.floor(this.card.cut[2]));
                var h = Math.max(1, Math.floor(this.card.cut[3]));
                c.drawImage(pic, this.card.cut[0], this.card.cut[1], w, h, pics[0], pics[1], pics[2], pics[3]);
                c.globalAlpha = this.card.alpha;
                if (this.card.flash && this.card.rarity != "N") {
                    c.drawImage(flash, this.card.cut[0], this.card.cut[1], w, h, pics[0], pics[1], pics[2], pics[3]);
                }
                c.globalAlpha = 1;
            }
        }

        //灵摆
        if (this.card.ifm.type3 == "lb") {
            var l1 = document.getElementById("l1");
            var l2 = document.getElementById("l2");
            c.drawImage(l1, 56, 734);
            c.drawImage(l2, 750, 735);
            c.fillStyle = "rgba(0,0,0,0.9)";
            var lb_num = parseInt(this.card.ifm.lb) || 0;
            if (lb_num > 9) {
                c.font = "42px number";
                c.fillText(lb_num, 65, 848);
                c.fillText(lb_num, 705, 848);
            }
            else {
                c.font = "45px number";
                c.fillText(lb_num, 77, 848);
                c.fillText(lb_num, 717, 848);
            }

            c.fillStyle = "#000";
            var value = this.card.ifm.lb_value;
            if (value) {
                var len = value.length;
                if (len <= 135) {
                    c.font = "25px " + lang;
                }
                else {
                    var s = 25 - (len - 210) / 50;
                    c.font = s + "px " + lang;
                }
                var desc = this.card.ansys_lb(value);
                for (j in desc) {
                    if (j < 5) {
                        c.fillText(desc[j], 128, 772 + j * 25, 560);
                    }
                }
            }
        }

        if (this.card.ifm.type == "monster") {
            c.fillStyle = "#000";
            var value = this.card.ifm.value;
            if (value) {
                var len = value.length;
                if (len <= 210) {
                    c.font = "24px " + lang;
                }
                else {
                    var s = 24 - (len - 210) / 50;
                    c.font = s + "px " + lang;
                }

                if (this.card.ifm.type2 == "rh" || this.card.ifm.type2 == "tt" || this.card.ifm.type2 == "cl" || this.card.ifm.type2 == "lj") {
                    var del = true;
                }
                else {
                    var del = false;
                }

                if (desc)
                    var desc = this.card.ansys(value, 6, del);

                for (j in desc) {
                    if (j < 6) {
                        c.fillText(desc[j], 62, 944 + j * 26, 690);
                    }
                }
            }
            c.fillStyle = "#000";
            c.font = "36px number";
            if (this.card.ifm.attack < 10 || this.card.ifm.attack == "?" || this.card.ifm.attack == "？") {
                if (this.card.ifm.attack == "？") {
                    this.card.ifm.attack = "?";
                }
                c.fillText(this.card.ifm.attack, 565, 1107);
            }
            else if (this.card.ifm.attack >= 10 && this.card.ifm.attack < 100) {
                c.fillText(this.card.ifm.attack, 548, 1107);
            }
            else if (this.card.ifm.attack >= 100 && this.card.ifm.attack < 1000) {
                c.fillText(this.card.ifm.attack, 531, 1107);
            }
            else {
                c.fillText(this.card.ifm.attack, 513, 1107);
            }
            if (this.card.ifm.type2 != "lj") {
                if (this.card.ifm.defend < 10 || this.card.ifm.defend == "?") {
                    c.fillText(this.card.ifm.defend, 723, 1107);
                }
                else if (this.card.ifm.defend >= 10 && this.card.ifm.defend < 100) {
                    c.fillText(this.card.ifm.defend, 709, 1107);
                }
                else if (this.card.ifm.defend >= 100 && this.card.ifm.defend < 1000) {
                    c.fillText(this.card.ifm.defend, 691, 1107);
                }
                else {
                    c.fillText(this.card.ifm.defend, 674, 1107);
                }
            };
            if (this.lang == "cn") {
                c.font = "27px " + lang;
                var left_ = 47;
            }
            else {
                c.font = "26px " + lang;
                var left_ = 61;
            }
            c.fillStyle = "#000";
            c.fillText(this.card.race_txt, left_, 916);
        }
        else {
            c.fillStyle = "#000";
            var value = this.card.ifm.value;
            if (value) {
                var len = value.length;
                if (len <= 210) {
                    c.font = "24px " + lang;
                }
                else {
                    var s = 24 - (len - 210) / 50;
                    c.font = s + "px " + lang;
                }

                if (this.card.ifm.type2 == "rh" || this.card.ifm.type2 == "tt" || this.card.ifm.type2 == "cl" || this.card.ifm.type2 == "lj") {
                    var del = true;
                }
                else {
                    var del = false;
                }

                var desc = this.card.ansys(value, 9, del);

                for (j in desc) {
                    if (j < 9) {
                        c.fillText(desc[j], 62, 914 + j * 26, 690);
                    }
                }
            }
            c.fillStyle = "#000";
            c.font = "47px " + lang;
            if (this.card.ifm.type == "magic") {
                if (this.card.ifm.type2 == "tc") {
                    if (this.lang == "cn") {
                        c.fillText("【魔法卡】", 533, 182);
                    }
                    else if (this.lang == "jp") {
                        c.fillText("【魔法カード】", 455, 185);
                    }
                    else {
                        c.font = "47px en_name";
                        c.fillText("[Spell Card]", 455, 185);
                    }
                }
                else {
                    if (this.lang == "cn") {
                        c.fillText("【魔法卡\r\r】", 489, 182);
                    }
                    else {
                        c.fillText("【魔法カード\r\r】", 431, 185);
                    }
                    var type2 = document.getElementById("type2");
                    c.drawImage(type2, 684, 148);
                }
            }
            else {
                if (this.card.ifm.type2 == "tc") {
                    if (this.lang == "cn") {
                        c.fillText("【陷阱卡】", 533, 182);
                    }
                    else {
                        c.fillText("【罠カード】", 510, 185);
                    }
                }
                else {
                    if (this.lang == "cn") {
                        c.fillText("【陷阱卡\r\r】", 489, 182);
                    }
                    else {
                        c.fillText("【罠カード\r\r】", 479, 185);
                    }
                    var type2 = document.getElementById("type2");
                    c.drawImage(type2, 684, 148);
                }
            }
        };
        if (this.card.ifm.type == "monster" && this.card.ifm.type2 != "cl" && this.card.ifm.type2 != "lj") {
            c.fillStyle = "#000";
        }
        else {
            c.fillStyle = "#fff";
        }
        c.font = "66px " + lang;
        if (lang == "en") {
            c.font = "67px en_name";
        }
        if (lang == "jp") {
            if (this.card.rarity == "N" || this.card.rarity == "SR") {
                c.fillText(this.card.ifm.name, 64, 121, 610);
            }
            else if (this.card.rarity == "UR") {
                c.fillStyle = "#473e0e";
                c.fillText(this.card.ifm.name, 64, 121, 610);
                c.globalAlpha = this.card.alpha;
                c.fillStyle = "#efea97";
                c.fillText(this.card.ifm.name, 64, 121, 610);
                c.globalAlpha = 0.8;
                c.strokeStyle = "#efea97";
                c.lineWidth = 1;
                c.strokeText(this.card.ifm.name, 64, 121, 610);
                c.globalAlpha = 1;
            }
            else if (this.card.rarity == "R") {
                c.fillStyle = "#404040";
                c.fillText(this.card.ifm.name, 64, 121, 610);
                c.globalAlpha = this.card.alpha;
                c.fillStyle = "#f1f0d7";
                c.fillText(this.card.ifm.name, 64, 121, 610);
                c.globalAlpha = 0.8;
                c.strokeStyle = "#f1f0d7";
                c.lineWidth = 1;
                c.strokeText(this.card.ifm.name, 64, 121, 610);
                c.globalAlpha = 1;
            }
        }
        else {
            if (this.card.rarity == "N" || this.card.rarity == "SR") {
                c.fillText(this.card.ifm.name, 64, 114, 610);
            }
            else if (this.card.rarity == "UR") {
                c.fillStyle = "#473e0e";
                c.fillText(this.card.ifm.name, 64, 114, 610);
                c.globalAlpha = this.card.alpha;
                c.fillStyle = "#efea97";
                c.fillText(this.card.ifm.name, 64, 114, 610);
                c.globalAlpha = 0.8;
                c.strokeStyle = "#efea97";
                c.lineWidth = 1;
                c.strokeText(this.card.ifm.name, 64, 114, 610);
                c.globalAlpha = 1;
            }
            else if (this.card.rarity == "R") {
                c.fillStyle = "#404040";
                c.fillText(this.card.ifm.name, 64, 114, 610);
                c.globalAlpha = this.card.alpha;
                c.fillStyle = "#f1f0d7";
                c.fillText(this.card.ifm.name, 64, 114, 610);
                c.globalAlpha = 0.8;
                c.strokeStyle = "#f1f0d7";
                c.lineWidth = 1;
                c.strokeText(this.card.ifm.name, 64, 114, 610);
                c.globalAlpha = 1;
            }
        }
        if (this.card.ifm.type == "monster" && this.card.ifm.type2 != "lj") {
            var stars = this.card.ifm.stars;
            if (this.card.ifm.type2 != "cl") {
                var star = document.getElementById("star");
                for (var i = 0; i < stars; i++) {
                    var left = 677 - i * 54;
                    c.drawImage(star, left, 146, 52, 52);
                }
            }
            else {
                var star = document.getElementById("level");
                for (var i = 0; i < stars; i++) {
                    var left = 82 + i * 54;
                    c.drawImage(star, left, 146, 52, 52);
                }
            }
        };
        var prop = document.getElementById("prop");
        c.drawImage(prop, 0, 0, 66, 66, 677, 55, 79, 79);
        if (this.card.holo) {
            var holo = document.getElementById("holo");
            var holo2 = document.getElementById("holo2");
            c.drawImage(holo, 752, 1126, 35, 34)
            c.globalAlpha = this.card.alpha;
            c.drawImage(holo2, 752, 1126, 35, 34);
            c.globalAlpha = 1;
        };
        if (this.card.key) {
            c.font = "23px number";
            var password = this.card.password;
            var t = 8 - password.length;
            for (var i = 0; i < t; i++) {
                password = "0" + password;
            }
            if (this.card.ifm.type2 != "cl") {
                c.fillStyle = "rgba(0,0,0,0.9)";
            }
            else {
                c.fillStyle = "rgba(255,255,255,0.9)";
            }
            c.fillText(password, 43, 1148);
        };
        if (this.card.card_bag) {
            c.font = "23px number";
            var bag = this.card.card_bag_num;
            if (this.card.ifm.type2 != "cl") {
                c.fillStyle = "rgba(0,0,0,0.9)";
            }
            else {
                c.fillStyle = "rgba(255,255,255,0.9)";
            }
            if (this.card.ifm.type3 == "lb") {
                c.fillText(bag, 70, 1106);
            }
            else if (this.card.ifm.type2 == "lj") {
                c.fillText(bag, 541, 872);
            }
            else {
                c.fillText(bag, 631, 872);
            }
        };
        if (this.card.ifm.type2 == "lj") {
            var l = 0;
            for (var i = 0; i < 8; i++) {
                if (this.card.ifm.links[i]) {
                    l++;
                }
            }
            c.font = "bolder 34px link";
            c.fillStyle = "rgba(0,0,0,0.9)";
            c.fillText(l, 719, 1108);
            var arrow = document.getElementById("arrow");
            var arrow1 = document.getElementById("arrow1");
            var arrow2 = document.getElementById("arrow2");
            var arrow12 = document.getElementById("arrow12");
            if (!this.card.ifm.links[6]) {
                c.drawImage(arrow, 337, 836);
            }
            else {
                c.drawImage(arrow2, 337, 836);
            }
            if (!this.card.ifm.links[5]) {
                c.drawImage(arrow1, 72, 795);
            }
            else {
                c.drawImage(arrow12, 72, 795);
            }
            c.translate(674, 865);
            c.rotate(-90 * Math.PI / 180);
            if (!this.card.ifm.links[7]) {
                c.drawImage(arrow1, 0, 0);
            }
            else {
                c.drawImage(arrow12, 0, 0);
            }
            c.translate(604, 70);
            c.rotate(-90 * Math.PI / 180);
            if (!this.card.ifm.links[2]) {
                c.drawImage(arrow1, 0, 0);
            }
            else {
                c.drawImage(arrow12, 0, 0);
            }
            c.translate(603, 70);
            c.rotate(-90 * Math.PI / 180);
            if (!this.card.ifm.links[0]) {
                c.drawImage(arrow1, 0, 0);
            }
            else {
                c.drawImage(arrow12, 0, 0);
            }
            c.translate(267, 41);
            c.rotate(0 * Math.PI / 180);
            if (!this.card.ifm.links[3]) {
                c.drawImage(arrow, 0, 0);
            }
            else {
                c.drawImage(arrow2, 0, 0);
            }
            c.translate(-239, -378);
            c.rotate(90 * Math.PI / 180);
            if (!this.card.ifm.links[1]) {
                c.drawImage(arrow, 0, 0);
            }
            else {
                c.drawImage(arrow2, 0, 0);
            }
            c.translate(-237, -379);
            c.rotate(90 * Math.PI / 180);
            if (!this.card.ifm.links[4]) {
                c.drawImage(arrow, 0, 0);
            }
            else {
                c.drawImage(arrow2, 0, 0);
            }
        };
    }
}

module.exports.Draw=Draw;